//
//  PGoodBarCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/19.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "PGoodBarCell.h"
#import "ArticleModel.h"
#import "PublishGoodViewController.h"

@implementation PGoodBarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    CGFloat interw = (ScreenWidth - 24 - 50  * 6) / 5;
    if (interw < 0)
        interw = 0;
    // Initialization code
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(12, 10, 50, 50);
    [self.button1 setTitle:@"图片" forState:UIControlStateNormal];
    self.button1.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 setImage:[UIImage imageNamed:@"pubaddimg"] forState:UIControlStateNormal];
    [self.button1 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button1 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(12 + interw + 50, 10, 50, 50);
    [self.button2 setTitle:@"拍照" forState:UIControlStateNormal];
    self.button2.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 setImage:[UIImage imageNamed:@"pubaddcam"] forState:UIControlStateNormal];
    [self.button2 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button2 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(12 + (interw + 50) * 2, 10, 50, 50);
    [self.button3 setTitle:@"文字" forState:UIControlStateNormal];
    self.button3.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 setImage:[UIImage imageNamed:@"pubaddtext"] forState:UIControlStateNormal];
    [self.button3 setTitleEdgeInsets:UIEdgeInsetsMake(24, -15, 0, 0)];
    [self.button3 setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 20, 0)];
    
    [self.contentView addSubview:self.button1];
    [self.contentView addSubview:self.button2];
    [self.contentView addSubview:self.button3];
    
    [self.button1 addTarget:self action:@selector(addImg) forControlEvents:UIControlEventTouchUpInside];
    [self.button2 addTarget:self action:@selector(addCam) forControlEvents:UIControlEventTouchUpInside];
    [self.button3 addTarget:self action:@selector(addText) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)addImg {
    NSInteger maxCount = 1;
    if (ISIOS7 || ISIOS8_0) {
        ELCImagePickerController *elcPicker = [ELCImagePickerController new];
        elcPicker.maximumImagesCount = maxCount;
        elcPicker.imagePickerDelegate = self;
        
        if (self.controller) {
            [self.controller presentViewController:elcPicker animated:YES completion:nil];
        }
    } else {
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.assetCollectionSubtypes =@[
                                                         @(PHAssetCollectionSubtypeSmartAlbumUserLibrary), //相机胶卷
                                                         @(PHAssetCollectionSubtypeAlbumMyPhotoStream), //我的照片流
                                                         @(PHAssetCollectionSubtypeSmartAlbumPanoramas), //全景图
                                                         @(PHAssetCollectionSubtypeSmartAlbumBursts), //连拍模式拍摄的照片
                                                         @(PHAssetCollectionSubtypeSmartAlbumFavorites)
                                                         ];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.mediaType = QBImagePickerMediaTypeImage;
        imagePickerController.maximumNumberOfSelection = maxCount;
        imagePickerController.showsNumberOfSelectedAssets = YES;
        if (self.controller) {
            [self.controller presentViewController:imagePickerController animated:YES completion:nil];
        }
    }
}

- (void)addCam {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    
    if (self.controller) {
        [self.controller presentViewController:picker animated:YES completion:^{
        }];
    }
}

- (void)addText {
    ArticleModel *baseone = [ArticleModel new];
    baseone.type = ArticleModelTxt;
    baseone.txt = @"";
    [[PublishData sharedPublishData].goodlocalArray addObject:baseone];
    if (self.controller) {
        PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
        [pb refreshTable];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    [picker dismissViewControllerAnimated:YES completion:^{
        ArticleModel *baseone = [ArticleModel new];
        baseone.type = ArticleModelImageTxt;
        baseone.txt = @"";
        baseone.img = image;
        [[PublishData sharedPublishData].goodlocalArray addObject:baseone];
        
        if (self.controller) {
            PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
            [pb refreshTable];
        }
    }];
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    //TODO:changeToData
    [picker dismissViewControllerAnimated:YES completion:^{
        for (int i = 0;i<[info count];i++) {
            UIImage *img = [[info objectAtIndex:i]objectForKey:@"UIImagePickerControllerOriginalImage"];
            ArticleModel *baseone = [ArticleModel new];
            baseone.type = ArticleModelImageTxt;
            baseone.txt = @"";
            baseone.img = img;
            [[PublishData sharedPublishData].goodlocalArray addObject:baseone];
            if (self.controller) {
                PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
                [pb refreshTable];
            }
        }
    }];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        DYLog(@"cancl");
    }];
}

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
    NSUInteger count = assets.count;
    __block NSUInteger select = 0;
    PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
    option.synchronous = YES;
    for (PHAsset *asset in assets) {
        [[PHCachingImageManager defaultManager]requestImageDataForAsset:asset options:option resultHandler:^(NSData * imageData, NSString * dataUTI, UIImageOrientation orientation, NSDictionary * info) {
            if(imageData) {
                ArticleModel *baseone = [ArticleModel new];
                baseone.type = ArticleModelImageTxt;
                baseone.txt = @"";
                baseone.img = [UIImage imageWithData:imageData];
                [[PublishData sharedPublishData].goodlocalArray addObject:baseone];
                if (self.controller) {
                    PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
                    [pb refreshTable];
                }
                select++;
            }
            if(select == count) {
                [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
                return;
            }
        }];
        
    }
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [imagePickerController dismissViewControllerAnimated:NO completion:NULL];
}

@end
